package model

import (
	"database/sql"
	"errors"
	"fmt"
	g "github.com/doug-martin/goqu/v9"
	"github.com/valyala/fasthttp"
)

type TblGradient struct {
	Id          int    `json:"id" db:"id" cbor:"id"`
	BusinessId  string `json:"business_id" db:"business_id" cbor:"business_id"`
	OperatorId  string `json:"operator_id" db:"operator_id" cbor:"operator_id"`
	Type        string `json:"type" db:"type" cbor:"type"`
	Switch      string `json:"switch" db:"switch" cbor:"switch"`
	JsonContent string `json:"json_content" db:"json_content" cbor:"json_content"`
}

type TblGradientParam struct {
	Page     int `json:"page"`
	PageSize int `json:"page_size"`

	Id          int    `json:"id" db:"id" cbor:"id"`
	BusinessId  string `json:"business_id" db:"business_id" cbor:"business_id"`
	OperatorId  string `json:"operator_id" db:"operator_id" cbor:"operator_id"`
	Type        string `json:"type" db:"type" cbor:"type"`
	Switch      string `json:"switch" db:"switch" cbor:"switch"`
	JsonContent string `json:"json_content" db:"json_content" cbor:"json_content"`
}

type TblGradientList struct {
	D []TblGradient `json:"d" cbor:"d"`
	T int           `json:"total" cbor:"t"`
}

func GetTblGradient(param TblGradientParam, ctx *fasthttp.RequestCtx) (TblGradient, error) {
	data := TblGradient{}
	table := "tbl_cfg_sys_gradient"
	where := "1=1"
	join := ""
	loginUser := GetLoginUser(ctx)
	if loginUser.Admin != "" {
		param.OperatorId = "888888"
	}
	if loginUser.Operator != "" {
		where += " and a.operator_id='" + loginUser.Operator + "'"
	}
	if loginUser.Businsess != "" {
		where += " and a.business_id='" + loginUser.Businsess + "'"
	}

	if param.BusinessId != "" {
		where += " and a.business_id='" + param.BusinessId + "'"
	}
	if param.OperatorId != "" {
		where += " and a.operator_id='" + param.OperatorId + "'"
	}
	if param.Type != "" {
		where += " and a.type='" + param.Type + "'"
	}

	field := "a.*"
	query := "select " + field + " from " + table + "  as a " + join + " where " + where
	err := meta.MerchantDB.Get(&data, query)
	if err != nil && err != sql.ErrNoRows {
		return data, pushLog(fmt.Errorf("%s,[%s]", err.Error(), query), "数据库错误")
	}
	return data, nil
}

func UpdateOrDelTblGradient(param TblGradient, isDelete int, ctx *fasthttp.RequestCtx) error {
	table := "tbl_cfg_sys_gradient"
	query := ""

	record := g.Record{
		"business_id":  "0",
		"operator_id":  "0",
		"type":         param.Type,
		"switch":       param.Switch,
		"json_content": param.JsonContent,
	}
	loginUser := GetLoginUser(ctx)

	if loginUser.Operator != "" {
		record["operator_id"] = loginUser.Operator
	} else if loginUser.Businsess != "" {
		record["business_id"] = loginUser.Businsess
	} else {
		record["operator_id"] = "888888"
	}
	if param.Id > 0 {
		has := TblGradient{}
		meta.MerchantDB.Get(&has, fmt.Sprintf("select * from tbl_cfg_sys_gradient where id='%d'", param.Id))
		if loginUser.Operator != "" {
			if has.OperatorId != loginUser.Operator {
				return errors.New("权限不足")
			}
		}
		if loginUser.Businsess != "" {
			if has.BusinessId != loginUser.Businsess {
				return errors.New("权限不足")
			}
		}

		if isDelete == 0 {
			query, _, _ = dialect.Update(table).Set(record).Where(g.Ex{"id": param.Id}).ToSQL()
		} else {
			query, _, _ = dialect.Delete(table).Where(g.Ex{"id": param.Id}).ToSQL()
		}
	} else {

		query, _, _ = dialect.Insert(table).Rows(record).ToSQL()
	}
	fmt.Println(query)
	_, err := meta.MerchantDB.Exec(query)
	if err != nil {
		return errors.New("数据库错误")
	}
	return nil
}
